package com.ccpdown.util.xml;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import com.ccpdown.vo.ConfiedText;
import com.ccpdown.vo.SoftList;
import com.ccpdown.vo.Software;

public class CtrXmlUtil {
	
	private static SAXParserFactory spf;
	private static SAXParser sp;
	private static XMLReader xr;
	static{
		try {
			spf=SAXParserFactory.newInstance();
			sp = spf.newSAXParser();
			xr=sp.getXMLReader();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public static List<ConfiedText> getTypes(InputStream is){
		List<ConfiedText> list=null;
		SAXParserFactory factory=SAXParserFactory.newInstance();
		try {
			list=new ArrayList<ConfiedText>();
			SAXParser parse = factory.newSAXParser();
			SAXHandler handler=new SAXHandler();
			//InputStream is=CtrXmlUtil.class.getResourceAsStream("type_2.xml");
			//putStream is= new FileInputStream(new File("type2.xml"));
			parse.parse(is, handler);
			list=handler.getCts();
			Collections.sort(list);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	/*
	根据path显示该XML的数据
	@path ·����;
	@flag, 1  显示软件类别列表 2 显示软件列表
	*/
	public static List getTypeFromPath(String path,int flag){
		List list=null;
		URL url=null;
		
		try {
			url=new URL(path);
			SAXHandler handler=new SAXHandler();
			sp.parse(new InputSource(url.openStream()), handler);
			if(flag==1){
				list=handler.getCts();
			}else if(flag==2){
				list=handler.getList();
				System.out.println();
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	//得到软件详细信息数据；
	public static Software getSoftware(String path){
		Software sw=null;
		URL url=null;
		try {
			url=new URL(path);
			SAXHandler handler=new SAXHandler();
			sp.parse(new InputSource(url.openStream()), handler);
			sw=handler.getSoftware();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sw;
	}
	
	//得到软件类别的总名称；
	public static String getTypeName(String path){
		String typeName=null;
		URL url=null;
		try {
			url=new URL(path);
			SAXHandler handler=new SAXHandler();
			sp.parse(new InputSource(url.openStream()), handler);
			typeName=handler.getAcctributesType();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return typeName;
	}
	
	//获得软件列表
	public static SoftList getSoftList(String path){
		SoftList softList=null;
		URL url=null;
		try {
			url=new URL(path);
			SAXHandler handler=new SAXHandler();
			sp.parse(new InputSource(url.openStream()), handler);
			softList=handler.getSoftList();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return softList;
	}
	
	public static Software getUpdate(String path){
		Software sw=null;
		URL url=null;
		
		try {
			url=new URL(path);
			SAXHandler handler=new SAXHandler();
			sp.parse(new InputSource(url.openStream()), handler);
			sw=handler.getSoftware();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		return sw;
	}
}
